package cn.icare.cms.controller.restful;

import cn.icare.cms.MenuService;
import cn.icare.cms.aop.UserLog;
import cn.icare.cms.model.vo.rest.RespMsgVO;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping(value = "/rest/menu")
@Api(tags = "REST菜单")
public class MenuController extends BaseController {
    @Resource
    private MenuService menuService;
    private final String FALL_STRING = "请求失败,服务繁忙!";

    /**
     * URL:http://{ip}:{port}/rest/menu
     * method:POST
     *
     * @param userGroupId
     * @param menuIds
     * @return
     */
    @PutMapping()
    @HystrixCommand(fallbackMethod = "updateMenuUserGroupFall")
    @UserLog(desc = "更新用户组菜单权限")
    @ApiOperation(value = "更新用户组菜单权限", notes = "先删除所有该用户组的菜单权限,然后新增该用户组所有的新菜单权限")
    public ResponseEntity<RespMsgVO> updateMenuUserGroup(@RequestParam(name = "userGroupId") Integer userGroupId,
                                                         @RequestParam("menuIds") List<Integer> menuIds) {
        if (this.menuService.updateUserPositionMenu(userGroupId, menuIds)) {
            return super.getRespOk();
        } else {
            return super.getRespFail();
        }
    }

    /**
     * URL:http://{ip}:{port}/rest/menu/{id}
     * method:GET
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @HystrixCommand(fallbackMethod = "getOneFall")
    @ApiOperation(value = "查询单个菜单", notes = "menu表返回符合主键的单个菜单")
    public ResponseEntity<RespMsgVO> getOne(@PathVariable("id") Integer id) {
        return super.getRespOk(this.menuService.getOne(id));
    }

    /**
     * URL:http://{ip}:{port}/rest/menu?args={args}
     * method:GET
     *
     * @return
     */
    @GetMapping()
    @HystrixCommand(fallbackMethod = "getListFall")
    @ApiOperation(value = "获取菜单权限", notes = "userGroupId不为空时,返回相应用户组所具备的菜单权限;userGroupId为空时,返回所有系统的菜单权限")
    public ResponseEntity<RespMsgVO> getList(@RequestParam(name = "userGroupId", required = false) Integer userGroupId) {
        return super.getRespOk(this.menuService.getList(userGroupId));
    }

    /**
     *URL:http://{ip}:{port}/rest/menu/getAllList
     *      * method:GET
     * @return
     */
    @GetMapping("/getAllList")
    @ApiOperation(value = "获取系统所有菜单", notes = "获取系统所有菜单")
    public ResponseEntity<RespMsgVO> getAllList(){
        return super.getRespOk(this.menuService.getAllList());
    }



    /**
     * URL:http://{ip}:{port}/rest/menu?args={args}
     * method:GET
     *
     * @return
     */
    @GetMapping("/getPositionMenuList")
    @ApiOperation(value = "获取岗位有权限菜单列表", notes = "获取岗位有权限菜单列表")
    public ResponseEntity<RespMsgVO> getPositionMenuList(@RequestParam(name = "positionId", required = true) Integer positionId) {
        return super.getRespOk(this.menuService.getPositionMenuList(positionId));
    }


    //region fallback methods

    private ResponseEntity<RespMsgVO> updateMenuUserGroupFall(Integer userGroupId, List<Integer> menuIds) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> getOneFall(Integer id) {
        return super.getRespFail(this.FALL_STRING);
    }

    private ResponseEntity<RespMsgVO> getListFall(Integer userGroupId) {
        return super.getRespFail(this.FALL_STRING);
    }

    //endregion
}
